import 'package:flutter/material.dart';
import 'package:webview_flutter/webview_flutter.dart';

class IndexStackPage extends StatefulWidget {
  const IndexStackPage({super.key});

  @override
  State<IndexStackPage> createState() => _IndexStackPageState();
}

class _IndexStackPageState extends State<IndexStackPage> {
  //第一步，先声明一个weviewController
  late WebViewController _controller1;
  //第一步，先声明一个weviewController
  late WebViewController _controller2;

  int _currentIndex = 0;

  void _changeCurrent(index) {
    setState(() {
      _currentIndex = index;
    });
  }

  @override
  void initState() {
    super.initState();
    //初始化控制器
    //第二步，在 initState 中初始化 controller
    _controller1 = WebViewController()
      //默认是不开启的，手动开启支持js
      ..setJavaScriptMode(JavaScriptMode.unrestricted)
      ..loadRequest(Uri.parse('https://www.csdn.net/'), headers: {});

    _controller2 = WebViewController()
      //默认是不开启的，手动开启支持js
      ..setJavaScriptMode(JavaScriptMode.unrestricted)
      ..loadRequest(Uri.parse('https://juejin.cn'), headers: {});
  }

  @override
  Widget build(BuildContext context) {
    return WillPopScope(
        child: Scaffold(
          appBar: AppBar(
            title: Text('测试多个webview后台运行'),
          ),
          body: IndexedStack(index: _currentIndex, children: [
            Column(
              children: [
                ElevatedButton(
                    onPressed: () {
                      setState(() {
                        _currentIndex = 1;
                      });
                    },
                    child: Text("跳转webview1")),
                ElevatedButton(
                    onPressed: () {
                      setState(() {
                        _currentIndex = 2;
                      });
                    },
                    child: Text("跳转webview2")),
                TextField(
                  onSubmitted: (value) {
                    print(value);
                  },
                )
              ],
            ),
            Webview1(controller1: _controller1, changeCurrent: _changeCurrent),
            webview2(controller2: _controller2, changeCurrent: _changeCurrent),
          ]),
        ),
        onWillPop: () {
          print("这里是控制是否返回的函数");
          if (_currentIndex == 0) {
            return Future.value(true);
          } else {
            setState(() {
              _currentIndex = 0;
            });
            return Future.value(false);
          }
        });
  }
}

class Webview1 extends StatefulWidget {
  Webview1({super.key, required this.controller1, required this.changeCurrent});

  WebViewController controller1;

  final ValueChanged<int> changeCurrent;
  @override
  State<Webview1> createState() => _Webview1State();
}

class _Webview1State extends State<Webview1> {
  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        SizedBox(
          height: 600,
          child: WebViewWidget(controller: widget.controller1),
        ),
        ElevatedButton(
            onPressed: () {
              widget.changeCurrent(0);
            },
            child: Text("可以点击右上角"))
      ],
    );
  }
}

class webview2 extends StatefulWidget {
  webview2({super.key, required this.controller2, required this.changeCurrent});
  WebViewController controller2;
  final ValueChanged<int> changeCurrent;

  @override
  State<webview2> createState() => _webview2State();
}

class _webview2State extends State<webview2> {
  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        SizedBox(
          height: 600,
          child: WebViewWidget(controller: widget.controller2),
        ),
        ElevatedButton(
            onPressed: () {
              widget.changeCurrent(0);
            },
            child: Text("可以点击右上角"))
      ],
    );
  }
}
